package cn.kgc.springcloud.Controller;

import cn.kgc.springcloud.service.FaqsService;
import cn.kgc.springcloudfaqs.R;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/faqs/faqs")
public class FaqsController {
    @Resource
    private FaqsService faqsService;


    //当10秒钟内，10次请求的错误比例达到60%，会出现熔断
    @HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback",commandProperties = {
            //启动熔断器
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),
            //请求数量
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),
            //统计时间
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),
            //错误百分比
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60"),
    })
    @GetMapping("/selectPage")
    public R<Map<String,Object>> selectPage(@RequestParam("pageNum") Integer pageNum, String info){
            if ("111".equals(info)){
                throw new RuntimeException("******info 不能负数");
            }
        System.out.println("pageNUM:"+pageNum+"info:"+info);
        Map<String, Object> map = faqsService.selectPage(pageNum,info);
        return R.ok(map).setCode(200);
    }

    public R<Map<String,Object>> paymentCircuitBreaker_fallback(Integer pageNum,String info)
    {
        return R.failed("id 不能是111，请稍后再试，/(ㄒoㄒ)/~~ ");
    }

}
